using System;
using System.Collections.Generic;
using System.Text;
using PerfectPaperPasswords;

namespace pppTest
{
    /*
     * The pppTest console program that can be used to test the pppNet class from a command prompt.
     * The console application uses a 64-character SequenceKey when passed as a parameter or 
     * if none is provided it will generate a new unique SequenceKey
     */
    class Program
    {
        static int maxColumn = 7;
        static int maxRow = 10;
        static int maxPassList = 3;
        static pppNet ppp = new pppNet(); //instantiate the pppNet object

        static void Main(string[] args)
        {
            if ((args.Length == 1) &&
                (args[0].Length == 64))
            { 
                ppp.SequenceKeyString = args[0]; //use the SequenceKey passed as a parameter on the commandline
            }
            else 
            {
                ppp.GenerateRandomSequenceKey(); //generate a new random SequenceKey
            }
            DisplaySequenceKey();
            LoopPasslists();
            Console.ReadLine();
        }

        private static void LoopPasslists()
        {
            for (int passList = 0; passList < maxPassList; passList++) //loop through passlists
            {
                ppp.RetrievePasscodes(passList); //retrieve the specified passlist
                Console.WriteLine("\n\nPPP Passcard Demo{0,29}\n", passList + 1); //display passlist number
                LoopHeader();
                LoopRows();
            }
        }

        private static void LoopRows()
        {
            for (int row = 0; row < maxRow; row++)
            {
                Console.Write(String.Format("{0,4}", (row + 1).ToString() + ":")); //show row number
                LoopColumns(row);
                Console.WriteLine();
            }
        }

        private static void LoopColumns(int row)
        {
            for (int column = 0; column < maxColumn; column++)
            {
                Console.Write(String.Format("{0,6}", ppp[column + row * maxColumn])); //show passcode
            }
        }

        private static void LoopHeader()
        {
            Console.Write("   ");
            for (int column = 0; column < maxColumn; column++)
            {
                Console.Write(String.Format("{0,6}", 
                    Char.ConvertFromUtf32(Char.ConvertToUtf32("A", 0) + column))); //show column title starting from "A"
            }
            Console.WriteLine();
        }

        private static void DisplaySequenceKey()
        {
            Console.WriteLine("SequenceKey:");
            Console.WriteLine("   {0}", ppp.SequenceKeyString);
        }
    }
}

